package cmd

import (
	"gitee.com/whilew/gen-go/pkg/cmd/env"
	"gitee.com/whilew/gen-go/pkg/cmd/model"
	"gitee.com/whilew/gen-go/pkg/genericiooptions"
	"github.com/spf13/cobra"
	"os"
)

type GenOptions struct {
	Arguments []string

	genericiooptions.IOStreams
}

func NewDefaultGenCommand() *cobra.Command {
	return NewDefaultGenCommandWithArgs(GenOptions{
		Arguments: os.Args,
		IOStreams: genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr},
	})
}

func NewDefaultGenCommandWithArgs(o GenOptions) *cobra.Command {
	return NewGenCommand(o)
}

func NewGenCommand(o GenOptions) *cobra.Command {
	cmds := &cobra.Command{
		Use:   "gen",
		Short: "generate code",
		Run:   runHelp,
		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
			//fmt.Println("gen PersistentPreRunE")
			return nil
		},
		PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
			//fmt.Println("gen PersistentPostRunE")
			return nil
		},
	}

	cmds.AddCommand(model.NewCmdModel(o.IOStreams))
	cmds.AddCommand(env.NewCmdEnv(o.IOStreams))
	return cmds
}

func runHelp(cmd *cobra.Command, args []string) {
	cmd.Help()
}
